package org.myabstract.graph.view;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.part.DrillDownAdapter;
import org.eclipse.ui.part.ViewPart;
import org.myabstract.graph.example.Factory;
import org.myabstract.graph.example.Namespace;
import org.myabstract.graph.example.Test;
import org.myabstract.graph.model.Graph;
import org.myabstract.graph.model.INodeType;
import org.myabstract.graph.model.Node;

public class TestView extends ViewPart {

	private TreeViewer viewer;
	private DrillDownAdapter drillDownAdapter;

	private void contributeToActionBars() {
		IActionBars bars = getViewSite().getActionBars();

		// bars.getMenuManager()
		IMenuManager manager = bars.getMenuManager();
		manager.add(new Separator());

		manager.add(new Separator());

		manager.add(new Separator());

		drillDownAdapter.addNavigationActions(manager);
		manager.add(new Separator());

		// bars.getToolBarManager()
		IToolBarManager toolBarManager = bars.getToolBarManager();
		drillDownAdapter.addNavigationActions(toolBarManager);

		toolBarManager.add(new Separator());

	}

	private void hookContextMenu() {
		MenuManager menuMgr = new MenuManager("#PopupMenu");
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				TestView.this.fillContextMenu(manager);
			}
		});

		Menu menu = menuMgr.createContextMenu(viewer.getControl());
		viewer.getControl().setMenu(menu);
		getSite().registerContextMenu(menuMgr, viewer);
	}

	/**
	 * adaug actiuni la contextMenu->(clickDreapta)
	 * */
	private void fillContextMenu(IMenuManager manager) {
		drillDownAdapter.addNavigationActions(manager);
		manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
		manager.add(copyAction);
	}

	private IAction copyAction;

	private void makeActions() {

		copyAction = new Action("copy") {
			public void run() {
				String info = "";
				StructuredSelection selection = (StructuredSelection) viewer
						.getSelection();
				Object obj = selection.getFirstElement();
				if (obj instanceof Node) {
					info = ((Node) obj).getType().getCopyData();
				}

				Clipboard cb = new Clipboard(Display.getDefault());
				TextTransfer textTransfer = TextTransfer.getInstance();
				cb.setContents(new Object[] { info },
						new TextTransfer[] { textTransfer });
			}
		};


	}

	public void keyTriggeredActions() {
		Control control = viewer.getControl();

		control.addKeyListener(new KeyListener() {

			@Override
			public void keyReleased(org.eclipse.swt.events.KeyEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void keyPressed(org.eclipse.swt.events.KeyEvent e) {
				// TODO Auto-generated method stub

				// 99 este codul pentru 'c' iar
				// stateMask este codul pentru CTRL

				if ((e.keyCode == 99) && (e.stateMask == 262144)) {
					copyAction.run();
				}

			}
		});
	}

	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub

		viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);

		drillDownAdapter = new DrillDownAdapter(viewer);

		viewer.setLabelProvider(new LabelProvider());
		viewer.setContentProvider(new ContentProvider());

		Graph.instance.setTypeClass(Namespace.class);

//		Graph.instance.setTypeClass(Factory.class);
		
		Test t = new Test("org.eclipse.core.runtime.adapters");
		t.createNamespace();

		viewer.setInput(Graph.instance);

		viewer.expandAll();

		makeActions();
		hookContextMenu(); // pentru clickDreapta
		contributeToActionBars();
		keyTriggeredActions();
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
		viewer.getControl().setFocus();
	}
}
